package com.zysoft.system.act;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zysoft.common.Constants;
import com.zysoft.system.entity.LoginAuthentication;

@Controller("indexController")
public class IndexController {

    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public String index(){
    	
        return "index";
    }
    
    @RequestMapping(value = "/login", method = {RequestMethod.GET})
    public String login(Model model){
    	if(!model.containsAttribute(Constants.COMMAND)) {
            model.addAttribute(Constants.COMMAND, new LoginAuthentication());
        }
        return "login";
    }
    
    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    public String login(Model model, @ModelAttribute("command") @Valid LoginAuthentication command, BindingResult result){
    	if(result.hasErrors()) {
            model.addAttribute(Constants.COMMAND, new LoginAuthentication());
            return login(model);
        }
        return "redirect:/index";
    }

}
